﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Logger.Core
{
    /// <summary>
    /// 日志核心基类
    /// </summary>
    internal abstract class LoggerBase : ILogger
    {
        private string _defaultLoggerName = DateTime.Now.ToString("yyyyMMddhh") + ".log";

        protected string MvcUrl = System.Web.HttpContext.Current.Server.MapPath("/Logger/"
               + System.Web.HttpContext.Current.Request.RequestContext.RouteData.Values["controller"].ToString()
               + "/"
               + System.Web.HttpContext.Current.Request.RequestContext.RouteData.Values["action"].ToString()
               + "/");
        protected abstract void InputLogger(string message, string fileName);

        #region ILogger 成员
        /// <summary>
        /// 记录代码运行时间
        /// </summary>
        /// <param name="message">消息</param>
        /// <param name="action">所测试的代码块</param>
        /// <param name="fileName">日志文件名</param>
        public void Logger_Timer(string message, Action action, string fileName)
        {
            StringBuilder str = new StringBuilder();
            Stopwatch sw = new Stopwatch();
            sw.Restart();
            str.Append(message);
            action();
            str.Append("代码段运行时间:" + sw.ElapsedMilliseconds + "毫秒");
            InputLogger(str.ToString(), string.IsNullOrWhiteSpace(fileName)
                ? "CodeTime" + _defaultLoggerName
                : fileName);
            sw.Stop();
        }

        /// <summary>
        /// 记录代码运行时间，日志文件名以codeTime开头的时间戳
        /// </summary>
        /// <param name="message">消息</param>
        /// <param name="action">所测试的代码块</param>
        public void Logger_Timer(string message, Action action)
        {
            Logger_Timer(message, action, null);
        }

        /// <summary>
        /// 记录代码运行异常
        /// </summary>
        /// <param name="message">消息</param>
        /// <param name="action">要添加try...catch的代码块</param>
        /// <param name="fileName">日志文件名</param>
        public void Logger_Exception(string message, Action action, string fileName)
        {
            try
            {
                action();
            }
            catch (Exception ex)
            {
                InputLogger("代码段出现异常,信息为" + ex.Message, string.IsNullOrWhiteSpace(fileName)
                ? "Exception" + _defaultLoggerName
                : fileName);
            }
        }

        /// <summary>
        /// 记录代码运行异常，日志文件名以Exception开头的时间戳
        /// </summary>
        /// <param name="message">消息</param>
        /// <param name="action">要添加try...catch的代码块</param>
        public void Logger_Exception(string message, Action action)
        {
            Logger_Exception(message, action, null);
        }

        public void Logger_Info(string message)
        {
            InputLogger(message, null);
        }

        public void Logger_Info(string message, string fileName)
        {
            InputLogger(message, string.IsNullOrWhiteSpace(fileName)
                 ? "Logger" + _defaultLoggerName
                 : fileName);
        }
        #endregion
    }
}
